<?php
/**
 * @Author: rocky
 * Date: 2019/9/28
 * Time: 11:22
 */


namespace app\shop\controller\api\v1;


use app\common\controller\BaseApiController;
use app\common\service\LatLngTranslate;
use app\shop\model\ShopGoods;
use app\common\facade\Token;
use app\shop\model\ShopGoodsCate;
use app\shop\resource\GoodListResource;

/**
 * 商品
 * Class Goods
 * @package app\shop\controller\api\v1
 */
class Goods extends BaseApiController
{
    /*
     * 获取商品分类
     */
    public function getCate()
    {
        $pid = $this->request->get('pid');
        $store_id = $this->request->get('store_id');
        $data = ShopGoodsCate::when($pid, function ($q) use ($pid) {
            $q->where('pid', $pid);
        }, function ($q) {
            $q->where('pid','>',0);
        })->where('status', 1)->where('sid',$store_id)->field('id,title')->where('is_deleted', 0)->select();
        $this->successCode($data);
    }

    /*
     * 商品列表
     */
    public function getList()
    {
        $cate_id = $this->request->get('cate_id');
        $store_id = $this->request->get('store_id');
        $data = ShopGoods::with(['cart'=>function($query)use($store_id){
            $query->where('store_id',$store_id);
        }])->where('cate_id', $cate_id)->select();
        $this->successCode(GoodListResource::collection($data));
    }
    
    /*
     * 商品列表-登录
     */
    public function getGoodList()
    {
        $cate_id = $this->request->get('cate_id');
        $store_id = $this->request->get('store_id');
        $data = ShopGoods::with(['cart'=>function($query)use($store_id){
            $query->where('store_id',$store_id)->where('uid',Token::id());
        }])->where('cate_id', $cate_id)->select();
        $this->successCode(GoodListResource::collection($data));
    }


    /*
     * 店铺商品列表
     */
    public function getStoreGoods()
    {
        $store_id = $this->request->get('store_id');
        $data = ShopGoods::where('sid',$store_id)->select();
//        halt($data);
        $this->successCode(GoodListResource::collection($data));
    }

}